//
//  YUIViewController+Task.swift
//  NewNGPlay
//
//  Created by YMac on 2017/12/13.
//  Copyright © 2017年 zchd. All rights reserved.
//

extension YUIViewController{
    open func yRunOnUIThread(task:@escaping () -> ()){
        DispatchQueue.main.async {
            if self.isAlive {
                task()
            }
        }
    }
    
    open func yRunOnUIThreadDelay(delay:TimeInterval,selector:Selector!) -> Timer{
        return HWWeakTimer.scheduledTimer(withTimeInterval: delay, target: self, selector: selector, userInfo: nil, repeats: false)
    }
    
    open func yRunOnUIThreadLoop(loopTime:TimeInterval,firstDelay:Bool = false,selector:Selector!) -> Timer{
        if !firstDelay {
            self.perform(selector)
        }
        return HWWeakTimer.scheduledTimer(withTimeInterval: loopTime, target: self, selector: selector, userInfo: nil, repeats: true)
    }
    
    open func yRunOnUIThreadDelay(delay:TimeInterval,block:@escaping () -> ()) -> Timer{
        return HWWeakTimer.scheduledTimer(withTimeInterval: delay, block: { (_) in
            block()
        }, userInfo: nil, repeats: false)
    }
    
    open func yRunOnUIThreadLoop(loopTime:TimeInterval,firstDelay:Bool = false,block:@escaping () -> ()) -> Timer{
        if !firstDelay {
            block()
        }
        return HWWeakTimer.scheduledTimer(withTimeInterval: loopTime, block: { (_) in
            block()
        }, userInfo: nil, repeats: true)
    }
}
